<?php
require_once __DIR__.'/ErrorCode.php';
class User
{
    //数据库链接句柄
    private $_db;
    
    /**
     * [构造方法]
     * @param [Object PDO] $_db [PDO链接句柄]
     */
    public function __construct($_db)
    {
        $this->_db=$_db;
    }
    /**
     * [用户登录]
     * @param  [type] $username [description]
     * @param  [type] $password [description]
     * @return [type]           [description]
     */
    public function login($username,$password)
    {
        if(empty($username)){
            throw new Exception('用户名不能为空',ErrorCode::USERNAME_CANNOT_EMPTY);
        }
        if(empty($password)){
            throw new Exception('密码不能为空',ErrorCode::PASSWORD_CANNOT_EMPTY);
        }
        
        $sql='SELECT * FROM `user` WHERE `username`=:username AND `password`=:password';
        $password=$this->_md5($password);
        $stmt=$this->_db->prepare($sql);
        $stmt->bindParam(':username',$username);
        $stmt->bindParam(':password',$password);
        $stmt->execute();
        $user=$stmt->fetch(PDO::FETCH_ASSOC);
        
        if(empty($user)){
            throw new Exception('用户名或密码错误',ErrorCode::USERNAME_OR_PASSWORD_INVALID);
        }
        
        unset($user['password']);
        return $user;
    }
    
    /**
     * [用户注册]
     * @param  [type] $username [description]
     * @param  [type] $password [description]
     * @return [type]           [description]
     */
    public function register($username,$password)
    {
        if(empty($username)){
            throw new Exception('用户名不能为空',ErrorCode::USERNAME_CANNOT_EMPTY);
        }
        //数据验证
        if($this->_isUsernameExists($username)){
            throw new Exception('用户名已存在',ErrorCode::USERNAME_EXISTS);
        }
        
        if(empty($password)){
            throw new Exception('密码不能为空',ErrorCode::PASSWORD_CANNOT_EMPTY);
        }
        
        //写入数据库
        $sql='INSERT INTO `user` VALUES(NULL,:username,:password,:createdAt)';
        $createdAt=date('Y-m-d H:i:s',time());
        //$createdAt=time();
        $stmt=$this->_db->prepare($sql);
        $stmt->bindParam(':username',$username);
        $pwd_md5=$this->_md5($password);
        $stmt->bindParam(':password',$pwd_md5);
        $stmt->bindParam(':createdAt',$createdAt);
        // $stmt->execute();
        //return $this->_isUsernameExists($username);
        
        if(!$stmt->execute()){
            throw new Exception('注册失败',ErrorCode::REGISTER_FAIL);
        }
        
        return [
            'userId'=>$this->_db->lastInsertId(),
            'username'=>$username,
            'password'=>$pwd_md5,
            'createdAt'=>$createdAt
        ];
    }
    
    /**
     * [_md5 MD5加密]
     * @param  [type] $string [description]
     * @param  string $key    [description]
     * @return [type]         [description]
     */
    private function _md5($string,$key='imooc')
    {
        return md5($string.$key);
    }
    
    /**
     * [_isUsernameExists 检测用户是否存在]
     * @param  [type]  $username [description]
     * @return boolean           [description]
     */
    private function _isUsernameExists($username)
    {
        // $exists=false;
        $sql='SELECT * FROM `user` Where `username`=:username';
        $stmt=$this->_db->prepare($sql);
        $stmt->bindParam(':username',$username);
        $stmt->execute();
        $result=$stmt->fetch(PDO::FETCH_ASSOC);
        return !empty($result);
    }
}
